#!/bin/bash
# this is the ifup/ifdown kvm script to attach the vm nics to the bridge
#
# openQRM Enterprise developed by openQRM Enterprise GmbH.
#
# All source code and content (c) Copyright 2014, openQRM Enterprise GmbH unless specifically noted otherwise.
#
# This source code is released under the GNU General Public License version 2, unless otherwise agreed with openQRM Enterprise GmbH.
# The latest version of this license can be found here: src/doc/LICENSE.txt
#
# By using this software, you acknowledge having read this license and agree to be bound thereby.
#
#           http://openqrm-enterprise.com
#
# Copyright 2014, openQRM Enterprise GmbH <info@openqrm-enterprise.com>
#

export OPENQRM_SERVER_BASE_DIR=@@OPENQRM_SERVER_BASE_DIR@@
export APP_NIC=@@OPENQRM_APP_NIC@@
export PATH=/usr/sbin:/usr/bin:/sbin:/bin

export OPENQRM_SOURCE_DIR="$OPENQRM_SERVER_BASE_DIR/openqrm/"
. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/kvm/include/openqrm-plugin-kvm-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/kvm/etc/openqrm-plugin-kvm.conf
if [ -f $OPENQRM_RESOURCE_PARAMETER_FILE ]; then
	. $OPENQRM_RESOURCE_PARAMETER_FILE
elif [ -f $OPENQRM_SERVER_BASE_DIR/openqrm/etc/openqrm-server.conf ]; then
	. $OPENQRM_SERVER_BASE_DIR/openqrm/etc/openqrm-server.conf
	. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-server-functions
	resource_id=0
	openqrm_server_get_config
	resource_openqrmserver=$OPENQRM_SERVER_IP_ADDRESS
	openqrm_web_protocol=$OPENQRM_WEB_PROTOCOL
fi
# define wget to use with https
if [ "$openqrm_web_protocol" == "https" ]; then
	WGET="wget -q --no-check-certificate"
else
	WGET="wget -q"
fi

# find out the name
VM_BASE_DIR=$(pushd $(dirname $0) > /dev/null; echo $PWD; popd > /dev/null)
VM_NAME=`basename $VM_BASE_DIR`
# get the net config of the vm
. $VM_BASE_DIR/net1


if [ ! -f "$VM_BASE_DIR/openqrm-resource.conf" ]; then
	# get vms resource parameters
	if ! $WGET -q -t 1 -T 4 -O $VM_BASE_DIR/openqrm-resource.conf "$openqrm_web_protocol://${resource_openqrmserver}/openqrm/action/resource-monitor.php?resource_command=get_parameter&resource_mac=$KVM_VM_MAC_1"; then
		echo "ERROR: Could not refresh VMs resource-parameters!" | logger
		exit -1
	fi
fi
. $VM_BASE_DIR/openqrm-resource.conf

# get ip config
eval $appliance_capabilities
if [ "$IPT" != "" ]; then
	IP_TOKEN=`echo $IPT | cut -d'=' -f1`
	if [ ! -f "$VM_BASE_DIR/openqrm-network.$APP_NIC.conf" ]; then
		echo "- getting network configuration for token $IP_TOKEN nic $APP_NIC" | logger
		if ! $WGET -O $VM_BASE_DIR/openqrm-network.$APP_NIC.conf "$openqrm_web_protocol://${resource_openqrmserver}/openqrm/boot-service/ip-mgmt-appliance-config.php?ip_mgmt_command=get_config&appliance_id=$appliance_id&ip_mgmt_nic_id=$APP_NIC&ip_mgmt_token=$IP_TOKEN"; then
			echo "ERROR: Could not get ip-configuration from openQRM-server at ${resource_openqrmserver}!" | logger
			exit -1
		fi
	else
		echo "- using exiting network configuration for token $IP_TOKEN nic $APP_NIC" | logger
	fi
	. $VM_BASE_DIR/openqrm-network.$APP_NIC.conf
else
	echo "NOTICE: No ip-configuration for VM available. Continuing with regular bridging ..." | logger
fi


if [ "$VLAN_ID" != "" ]; then

	echo "NOTICE: if-down - got ip-configuration for VM -> $APP_NIC nic to $VLAN_ID ..." | logger
	# find out which interface to use according to which bridge is used
	# get the brigdge, get the phys ethx interface from the bridge
	PHYS_MAC=`brctl showmacs @@OPENQRM_PLUGIN_KVM_BRIDGE@@ | grep yes | awk '{ print $2 }' | head -n1`
	if [ "$PHYS_MAC" == "" ]; then
		echo "ERROR: Could not find the mac address of the phys. interface of @@OPENQRM_PLUGIN_KVM_BRIDGE@@!" | logger
		exit -1
	fi
	echo "NOTICE: Found phys. interface mac adress $PHYS_MAC belongs to @@OPENQRM_PLUGIN_KVM_BRIDGE@@!" | logger
	PHYS_NIC=`ifconfig -a | grep -i $PHYS_MAC | grep eth | awk '{ print $1 }' | head -n1`
	if [ "$PHYS_NIC" == "" ]; then
		echo "ERROR: Could not find the phys. interface of @@OPENQRM_PLUGIN_KVM_BRIDGE@@!" | logger
		exit -1
	fi

	# remove the tap from the bridge
	brctl delif brvlan$VLAN_ID $1

	if brctl show | grep -A1 brvlan$VLAN_ID | grep -A1 $PHYS_NIC.$VLAN_ID | grep tap &>/dev/null; then
		echo "NOTICE: Not removing brvlan$VLAN_ID because it is still in use ..." | logger
	else
		echo "NOTICE: removing $PHYS_NIC.$VLAN_ID from brvlan$VLAN_ID" | logger
		brctl delif brvlan$VLAN_ID $PHYS_NIC.$VLAN_ID
		echo "NOTICE: removing brvlan$VLAN_ID" | logger
		ifconfig brvlan$VLAN_ID down
		brctl delbr brvlan$VLAN_ID
		echo "NOTICE: removing $PHYS_NIC.$VLAN_ID" | logger
		vconfig rem $PHYS_NIC.$VLAN_ID
	fi
fi

# remove the tap dev
ifconfig $1 0.0.0.0 down
sleep 2






